<?php

namespace app\admin\model;
use think\Session;
use think\Model;

class Schedule extends Model
{

    // 表名
    protected $name = 'schedule';
    protected $pk = 'id';


    // 定义全局的查询范围
    protected function base($query)
    {
            //学校筛选
            $admin =Session::get("admin");
            if($admin["branchids"]!=null && $admin["branchids"]!='')
            {
                $query->where('branchids','in',$admin["branchids"]);     
            }
    }

    public function getStatuslist()
    {
        return ['0' => __('未开始'), '1' => __('已结束'), '2' => __('已关闭')];
    }

    public function getStarttimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['starttime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['endtime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getRenderAttr($value, $data)
    {
        $allDay = ($data['starttime'] === $data['endtime'] && date("H:i:s", $data['starttime']) == '00:00:00');
        return [
            'id'              => $data['id'],
            'title'           => '['.$data['branchname'].'] '.$data['classname'].'-'.$data['classroomname'].' - '.$data['teachername'],
            'start'           => date("c", $data['starttime']),
            'end'             => date("c", $data['endtime']),
            'backgroundColor' => '#3c8dbc',
            'borderColor'     => '',
            'allDay'          => $allDay,
            'url'             => 'schedule/view/ids/'.$data['id'],
            'className'       => "btn-dialog fc-{$data['status']}" . (($allDay ? $data['endtime'] + 86400 : $data['endtime']) < time() ? ' fc-expired' : '')
        ];
    }

    protected function setStarttimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setEndtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }
    

}
